//
//  ViewController.m
//  OpenEmail
//
//  Created by gongjipeng on 2021/5/12.
//
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"

@interface ViewController ()<SKPSMTPMessageDelegate>
@property (nonatomic,strong) SKPSMTPMessage * myEmail;
@property (nonatomic,strong) UILabel * centerLabel;

@end

@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self createEmail];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)createEmail{
    self.centerLabel.text = @"开始发送邮件 -> loding....";
    _myEmail = [[SKPSMTPMessage alloc] init];
    // 发送者信息
    _myEmail.fromEmail = @"18270686898@163.com"; //
    _myEmail.login = @"18270686898@163.com";
//    _myEmail.pass = @"Gongshaopeng123";
    _myEmail.pass = @"ABYKHKESAAOHBUUQ";
    _myEmail.relayHost = @"smtp.163.com"; //

    // 接收者信息
    _myEmail.toEmail = @"864659076@qq.com"; //

    _myEmail.requiresAuth = YES;
    _myEmail.wantsSecure = YES;
    _myEmail.delegate = self;
    
    // 主题
    _myEmail.subject = [NSString stringWithFormat:@"恭喜打卡成功！%@",[self currentTime]];
    
    
    // 正文
    NSString *content = [NSString stringWithCString:"You are a genius." encoding:NSUTF8StringEncoding];
   
    NSDictionary *plainPart = @{kSKPSMTPPartContentTypeKey : @"text/plain;charset=UTF-8",
                                kSKPSMTPPartMessageKey : content,
                                kSKPSMTPPartContentTransferEncodingKey : @"8bit",
                                };
    
    
    
    

    // 执行发送邮件代码：
    [_myEmail setParts:@[plainPart]]; // 邮件首部字段、邮件内容格式和传输编码
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self->_myEmail send];
        [[NSRunLoop currentRunLoop] run];
    });

}

#pragma mark - SKPSMTPMessageDelegate
- (void)messageSent:(SKPSMTPMessage *)message
{
  NSLog(@"恭喜,邮件发送成功");
    dispatch_sync(dispatch_get_main_queue(), ^{
        self.centerLabel.text = @"恭喜,邮件发送成功";
    });
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    NSLog(@"不好意思,邮件发送失败");
    self.centerLabel.text = @"不好意思,邮件发送失败";

}
#pragma mark - 获取当前时间
-(NSString *)currentTime{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获取当前时间日期展示字符串 如：2019-05-23-13:58:59
    NSString *str = [formatter stringFromDate:date];
    return str;
}
#pragma mark - 天气预报
-(void)weather{
//    NSString *googleURL =@"http://www.weather.com.cn/data/cityinfo/101070101.html";
//     NSURL *url = [NSURL URLWithString:googleURL];
//     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
//     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}
-(UILabel *)centerLabel{
    if (!_centerLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.frame = CGRectMake(0, (kScreenHeight -60)/2, kScreenWidth, 60);
        label.textColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:18];
        label.textAlignment = NSTextAlignmentCenter;
        _centerLabel = label;
        [self.view addSubview:_centerLabel];
    }
    return _centerLabel;
}

@end
